package com.efounder.view;

import android.content.Context;
import android.content.res.TypedArray;
import androidx.annotation.IntDef;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.efounder.mobilemanager.R;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * Created by Marcello on 2019/11/8
 * 通用titlebar,使用时先设置左右的view 在设置中间的view防止中间view位置偏差
 */
public class CommonTitleBar extends BaseTitleBar {
    protected int centerType;                             // 中间视图类型
    protected String centerText;                          // 中间TextView文字
    protected int centerTextColor;                        // 中间TextView字体颜色
    protected float centerTextSize;                       // 中间TextView字体大小
    protected int centerCustomViewRes;                    // 中间自定义布局资源
    protected View centerCustomView;                      // 中间自定义视图

    protected TextView tvCenter;                          // 标题栏文字
    private LinearLayout llMainCenter;

    public static final int TYPE_CENTER_NONE = 0;
    public static final int TYPE_CENTER_TEXTVIEW = 1;
    public static final int TYPE_CENTER_CUSTOM_VIEW = 2;

    public static final int ACTION_CENTER_TEXT = 5;     // 中间文字点击


    @IntDef({TYPE_CENTER_NONE, TYPE_CENTER_TEXTVIEW, TYPE_CENTER_CUSTOM_VIEW})
    @Retention(RetentionPolicy.SOURCE)
    public @interface CenterTypeDef {
    }
    public CommonTitleBar(Context context) {
        this(context, null);
    }

    public CommonTitleBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CommonTitleBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void loadAttributesCenterView(TypedArray array) {
        //中间的布局
        centerType = array.getInt(R.styleable.CommonTitleBar_centerType, TYPE_CENTER_TEXTVIEW);
        if (centerType == TYPE_CENTER_TEXTVIEW) {
            centerText = array.getString(R.styleable.CommonTitleBar_centerText);
            centerTextColor = array.getColor(R.styleable.CommonTitleBar_centerTextColor, getResources().getColor(R.color.title_TextColor));
            centerTextSize = array.getDimension(R.styleable.CommonTitleBar_centerTextSize, dpToPx(18f));
        } else if (centerType == TYPE_CENTER_CUSTOM_VIEW) {
            centerCustomViewRes = array.getResourceId(R.styleable.CommonTitleBar_centerCustomView, 0);
        }
    }

    @Override
    protected void initMainCenterViews(Context context) {
        if (centerType == TYPE_CENTER_NONE) {
            return;
        }
        if (centerType == TYPE_CENTER_TEXTVIEW) {
            // 初始化中间子布局
            llMainCenter = new LinearLayout(context);
            llMainCenter.setId(generateViewId());
            llMainCenter.setGravity(Gravity.CENTER);
            llMainCenter.setOrientation(LinearLayout.VERTICAL);
            llMainCenter.setPadding(PADDING_50, 0, PADDING_50, 0);
            llMainCenter.setOnClickListener(this);

            RelativeLayout.LayoutParams centerParams = new RelativeLayout.LayoutParams(WRAP_CONTENT, MATCH_PARENT);
            centerParams.addRule(RelativeLayout.CENTER_IN_PARENT);
            rlMain.addView(llMainCenter, centerParams);

            // 初始化标题栏TextView
            tvCenter = new TextView(context);
            tvCenter.setText(centerText);
            tvCenter.setTextColor(centerTextColor);
            tvCenter.setTextSize(TypedValue.COMPLEX_UNIT_PX, centerTextSize);
            tvCenter.setGravity(Gravity.CENTER);
            tvCenter.setSingleLine(true);
            tvCenter.setEllipsize(TextUtils.TruncateAt.END);
            tvCenter.setOnClickListener(this);
            LinearLayout.LayoutParams centerTextParams = new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
            llMainCenter.addView(tvCenter, centerTextParams);


        } else if (centerType == TYPE_CENTER_CUSTOM_VIEW) {
            // 初始化中间自定义布局
            centerCustomView = LayoutInflater.from(context).inflate(centerCustomViewRes, rlMain, false);
            if (centerCustomView.getId() == View.NO_ID) {
                centerCustomView.setId(generateViewId());
            }
            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) centerCustomView.getLayoutParams();
            layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
            rlMain.addView(centerCustomView);
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v.equals(tvCenter)) {
            if (titleBarListener!=null){
                titleBarListener.onClicked(v,ACTION_CENTER_TEXT);
            }
        }
    }

    public LinearLayout getCenterLayout() {
        return llMainCenter;
    }

    /**
     * @param centerText 设置中间布局的text
     */
    public CommonTitleBar setCenterTextViewText(String centerText) {
        this.centerType = TYPE_CENTER_TEXTVIEW;
        this.centerText = centerText;
        if (tvCenter != null) {
            tvCenter.setText(centerText);
        } else {
            clearCenterViews();
            initMainCenterViews(getContext());
        }
        return this;
    }

    /**
     * 获取标题栏中间TextView，对应centerType = textView
     *
     * @return
     */
    public TextView getCenterTextView() {
        return tvCenter;
    }


    //remove 中间多余的布局
    private void clearCenterViews() {
        if (centerCustomView != null) {
            rlMain.removeView(centerCustomView);
        }else if(tvCenter!=null){
            rlMain.removeView(tvCenter);
        }

    }

    /**
     * @param centerCustomViewRes 对应centerType = TYPE_CENTER_CUSTOM_VIEW 设置自定义布局
     */
    public CommonTitleBar setCenterCustomViewRes(int centerCustomViewRes) {
        centerType = centerCustomViewRes;
        clearCenterViews();
        initMainCenterViews(getContext());
        return this;
    }
    /**
     * @param centerType
     */
    public BaseTitleBar setCenterType(@CenterTypeDef int centerType) {
        this.centerType = centerType;
        clearCenterViews();
        initMainCenterViews(getContext());
        return this;
    }

    /**
     * @param centerView
     */
    public void setCenterView(View centerView) {
        if (centerView.getId() == View.NO_ID) {
            centerView.setId(generateViewId());
        }
        clearCenterViews();
        LayoutParams layoutParams = (LayoutParams) centerView.getLayoutParams();
        layoutParams.addRule(CENTER_IN_PARENT);
        rlMain.addView(centerView);
    }

    /**
     * 获取中间自定义布局视图
     *
     * @return
     */
    public View getCenterCustomView() {
        return centerCustomView;
    }


}
